package com.village.friend.pay.schedule;

import com.village.friend.constant.PayStatus;
import com.village.friend.constant.PayWayType;
import com.village.friend.entity.UserOrder;
import com.village.friend.pay.alipay.AliPayServiceImpl;
import com.village.friend.pay.wechat.WxPayServiceImpl;
import com.village.friend.service.OpenImService;
import com.village.friend.service.UserOrderService;
import com.village.friend.service.UserPolicyService;
import com.village.friend.service.UserService;
import com.village.friend.utils.OrderUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class SaticScheduleTask {

    @Autowired
    UserOrderService orderService;

    @Autowired
    UserService userService;

    @Autowired
    UserPolicyService policyService;

    @Autowired
    AliPayServiceImpl aliPayService;

    @Autowired
    WxPayServiceImpl wxPayService;

    @Autowired
    OpenImService openImService;

    //3.添加定时任务 Seconds Minutes Hours DayofMonth Month DayofWeek Year
    //cron表达式 详解 https://www.cnblogs.com/dyppp/p/7498457.html
    @Scheduled(cron = "0 0 0/1 * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void orderQueryTasks() {
        log.info("执行静态定时任务时间: " + LocalDateTime.now());
        log.info("加载处理中的订单");
        List<UserOrder> orderList = orderService.listByStatus(PayStatus.IN_PROCESS);
        log.info("加载到" + (orderList != null ? orderList.size() : 0) + "条数据");
        if (orderList != null) {
            for (UserOrder order :
                    orderList) {
                String payWayName = order.getPayWayName();
                String status = order.getPayStatus();
                if (PayWayType.WECHAT.getType().equalsIgnoreCase(payWayName)) {
                    //微信支付查单
                    wxPayService.wxQuery(order);
                } else if (PayWayType.ALIPAYAPP.getType().equalsIgnoreCase(payWayName)) {
                    //支付宝App支付查单
                    aliPayService.payQuery(order);
                } else if (PayWayType.ALIPAYH5.getType().equalsIgnoreCase(payWayName)) {
                    //支付宝H5支付查单
                    aliPayService.payQuery(order);
                }
                //订单状态和原来不一样
                if (!order.getPayStatus().equalsIgnoreCase(status)) {
                    //并且新的状态是成功 需要为缴费做对应的处理
                    if (PayStatus.SUCCESS.getValue().equalsIgnoreCase(order.getPayStatus())) {
                        PayStatus payStatus = PayStatus.valueTrans(order.getPayStatus());
                        OrderUtils.payStatusChange(orderService, userService, policyService, openImService, order.getId(), order.getOrderAmount(), payStatus);
                    } else {
                        //更新订单状态
                        orderService.updateById(order);
                    }
                }
                //延迟一秒
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
